package com.vsc.print_service.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class MessageUtils {
    static Properties properties = null;
    static Logger logger = LoggerFactory.getLogger(MessageUtils.class);

    public static void loadMessage() throws UnsupportedEncodingException, IOException {
        properties = new Properties();
        properties.load(new InputStreamReader(MessageUtils.class.getResourceAsStream("/message.properties"), "UTF-8"));
    }

    public static String getMessage(String key, String... argv) {
        if (properties == null) {
            try {
                loadMessage();
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }
        String message = properties.getProperty(key);

        if (argv != null) {
            for (String arg : argv) {
                message = message.replaceFirst("\\{[0-9]*\\}", arg);
            }
        }

        return message;
    }
}
